package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.model.mapper.VipBankInfoMapper;
import com.ttg.model.pojo.VipBankInfo;
import com.ttg.web.dao.VipBankInfoDao;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.List;


@Repository
public class VipBankInfoDaoImpl extends ServiceImpl<VipBankInfoMapper, VipBankInfo> implements VipBankInfoDao {

    @Override
    public List<VipBankInfo> getByVipIdAndSettingTime(String vipId, LocalDate settingTime) {
        LambdaQueryWrapper<VipBankInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VipBankInfo::getVipId,vipId);
        wrapper.between(VipBankInfo::getTime,settingTime,LocalDate.now());
        return list(wrapper);
    }

    @Override
    public VipBankInfo getLastOneByDate(Long vipId) {
        LambdaQueryWrapper<VipBankInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VipBankInfo::getVipId,vipId);
        wrapper.orderByDesc(VipBankInfo::getTime);

        return baseMapper.selectOne(wrapper);
    }

    @Override
    public List<VipBankInfo> getByLately(Long vipId) {
        LambdaQueryWrapper<VipBankInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VipBankInfo::getVipId,vipId);
        wrapper.eq(VipBankInfo::getTime,LocalDate.now());
        List<VipBankInfo> list = list(wrapper);
        if (CollectionUtils.isEmpty(list)){
            LambdaQueryWrapper<VipBankInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VipBankInfo::getVipId,vipId);
            queryWrapper.eq(VipBankInfo::getTime,LocalDate.now().minusDays(1));
            return list(queryWrapper);
        }
        return list;
    }

    @Override
    public List<VipBankInfo> getByVipIdAndTime(Long vipId, LocalDate settingTime, LocalDate endTime) {
        LambdaQueryWrapper<VipBankInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VipBankInfo::getVipId,vipId);
        wrapper.between(VipBankInfo::getTime,settingTime,endTime);
        return list(wrapper);
    }

    @Override
    public VipBankInfo getByAmountAndDate(String amount, LocalDate date) {
        LambdaQueryWrapper<VipBankInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VipBankInfo::getBankCarNo,amount);
        wrapper.eq(VipBankInfo::getTime,date);
        return getOne(wrapper);
    }
}
